package com.example.demo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class HelloApplication_combox extends Application {
    private final ComboBox<Dept> account = new ComboBox<>();
    private  final ObservableList<Dept> deptList = FXCollections.observableArrayList(Dept.DEPT_LIST);
    @Override
    public void start(Stage primaryStage) throws Exception {
        Label accountsLabel = new Label("Account:");
        account.setPrefWidth(200);
        account.setItems(deptList);
        Button saveButton = new Button("Save");

        HBox hbox = new HBox(
                accountsLabel,
                account,
                saveButton);
        hbox.setSpacing( 10.0d );
        hbox.setAlignment(Pos.CENTER );
        hbox.setPadding( new Insets(40) );

        Scene scene = new Scene(hbox);

        initCombo();

        saveButton.setOnAction( (evt) -> {
            System.out.println(account.getSelectionModel().getSelectedIndex());
            System.out.println(account.getSelectionModel().getSelectedItem());
            System.out.println(account.getValue());

//            System.out.println(evt);
//            if( account.getValue().equals(EMPTY_PAIR) ) {
//                System.out.println("no save needed; no item selected");
//            } else {
//                System.out.println("saving " + account.getValue());
//            }
        });

        primaryStage.setTitle("CombosApp");
        primaryStage.setScene( scene );
        primaryStage.show();

    }

    private void initCombo() {
        account.setConverter( new StringConverter<Dept>() {

            @Override
            public String toString(Dept dept) {
                return  dept==null ? "Select an DEPT" : dept.getDname();
            }

            @Override
            public Dept fromString(String s) {
                return null;
            }
        });
    }

    public static void main(String[] args) {
        launch();
    }
}